/*	CS362 Team 8 - Darren Mui, Preson Skaggs

	What discardCard() should do (based on info given in Piazza): Trash a card or put it
	into playedCards[]. All cards in playedCards[] should go into discard pile at end of
	turn.
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

#define NUM_TESTS 10000
#define ASSERTION_TEST 1

int main() {
	int currPlayer, numPlayer;
    int x, p, r;
	int i, j;
	int testCount;
    int k[10];
	int handPos, trash;
	int handBef, handAft, playedBef, playedAft;
    struct gameState G;

	SelectStream(2);
	PutSeed(-1); // seed based on system clock

    printf ("TESTING discardCard():\n");
    for (testCount = 0; testCount < NUM_TESTS; testCount++) {

		//randomize the 10 kingdom cards from adventurer to treasure_map
		for (i = 0; i < 10; i++) {
			k[i] = floor( (Random() * (treasure_map - adventurer + 1)) + adventurer);
		}	
		//randomize duplicates until there are no more duplicate kingdom cards
		for (i = 0; i < 10; i++) {
			for (j = 0; j < 10; j++) {
				while (k[i] == k[j] && i != j) {
					k[i] = floor( (Random() * (treasure_map - adventurer + 1)) + adventurer);
					j = 0;
				}
			}
		}

		// randomize number of players between 0 to MAX_PLAYERS
		do {
			numPlayer = floor(Random() * MAX_PLAYERS + 1);
		} while (numPlayer < 2);

		memset(&G, 23, sizeof(struct gameState));   // clear the game state
		r = initializeGame(numPlayer, k, floor(Random() * 256), &G); // initialize a new game

		// randomize deck, discard, and hand of every player
		for (currPlayer = 0; currPlayer < G.numPlayers; currPlayer++) {
			G.deckCount[currPlayer] = floor(Random() * MAX_DECK); // randomize # of cards in deck for player
			//set each card in the player deck to a random card from curse to treasure_map (based on the enum in dominion.h)
			for (i = 0; i < G.deckCount[currPlayer]; i++) {
				G.deck[currPlayer][i] = floor(Random() * (treasure_map + 1));
			}

			G.discardCount[currPlayer] = floor(Random() * MAX_DECK); // randomize # of cards in discard for player
			//set each card in the player discard to a random card from curse to treasure_map (based on the enum in dominion.h)
			for (i = 0; i < G.discardCount[currPlayer]; i++) {
				G.discard[currPlayer][i] = floor(Random() * (treasure_map + 1));
			}

			G.handCount[currPlayer] = floor(Random() * MAX_HAND); // randomize # of cards in player hand
			//set each card in the player hand to a random card from curse to treasure_map (based on the enum in dominion.h)
			for (i = 0; i < G.handCount[currPlayer]; i++) {
				G.hand[currPlayer][i] = floor(Random() * (treasure_map + 1));
			}
		}

		// randomize whose turn it is
		G.whoseTurn = floor(Random() * G.numPlayers); 
		p = G.whoseTurn;

		handPos = floor(Random() * G.handCount[p]); // randomize hand pos for current player's turn
		trash = floor(Random() * 2); // randomize from 0 to 1

		// hand and played count before discard
		handBef = G.handCount[p]; 
		playedBef = G.playedCardCount;

		x = discardCard(handPos, p, &G, trash);

		// hand and played count after discard
		handAft = G.handCount[p];
		playedAft = G.playedCardCount;

		#if (ASSERTION_TEST == 1)
			assert(x == 0); // successful call to discardCard()
			assert(handAft == (handBef - 1)); // handCount decremented
			if (trash == 0) 
				assert(playedAft == (playedBef + 1)); // playedCards incremented
			else
				assert(playedAft == playedBef); // card trashed
		#endif

    }

	printf("%d tests completed successfully\n", testCount);

    return 0;
}
